//
//  LZTNetworkTools.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/12.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit
import AFNetworking

// 网络请求枚举
enum LZTNetworkToolsMethod: String {
    // GET 请求
    case GET = "GET"
    // POST 请求
    case POST = "POST"
}
/*
 - 对afn进行封装
 - 提供一个可以发送get和post请求的公共方法
 - 帮助当前app 请求所有接口数据
 */
class LZTNetworkTools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools:LZTNetworkTools = {
        let tools = LZTNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    /**
     网络请求公共方法
     
     - parameter method:     请求方式
     - parameter urlString:  请求urlString
     - parameter parameters: 请求参数
     - parameter success:    请求成功的闭包
     - parameter failure:    请求失败的闭包
     */
    func request(method: LZTNetworkToolsMethod, urlString: String, parameters: AnyObject?, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // get请求
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                // 03
                success(response: response)
                }, failure: { (_, error) -> Void in
                    // 03
                    failure(error: error)
            })
            
        }else {
            // post 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                // 03
                success(response: response)
                }, failure: { (_, error) -> Void in
                    // 03
                    failure(error: error)
            })
        }
    }
}

//发布 按钮对应的extension
extension LZTNetworkTools {
    
    //发布 文字微博
    func composeUpdate(status: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
            "access_token": LZTUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        
        //发送请求
        request(LZTNetworkToolsMethod.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    //发布图片微博
    func composeUpload(status: String, images: [UIImage], success:(response: AnyObject?)->(), failure:(error: NSError)->()) {
        
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let params = [
            "access_token": LZTUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        
        //发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) in
            
            let data = UIImagePNGRepresentation(images.first!)!
            
            formdata.appendPartWithFileData(data, name: "pic", fileName: "suiyi", mimeType: "application/octet-stream")

            }, progress: nil, success: { (_, response) in
                
                success(response: response)
                
            }) { (_, error) in
                failure(error: error)
        }
        
    }
}

extension LZTNetworkTools {
    
    func homeLoadData(sinceId:Int64, maxId:Int64, success:(response: AnyObject?)->(), failure:(error: NSError)->()) {
        
        //url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求参数
        let params = [
            "access_token": LZTUserAccountViewModel.sharedTools.userAccount!.access_token!,
            "since_id":"\(sinceId)",
            "max_id": "\(maxId)"
        ]
        request(LZTNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
}

//将外面的请求获取 全部抽取到networkTools中来，以免暴露请求参数等
// 授权界面接口
extension LZTNetworkTools {
    
    //请求授权
    func oauthLoadUserAccount(code: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()) {
        
        //请求的url
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = [
            
            "client_id":WBAppKey,
            "client_secret":WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":WBREDIRECT_URI
        ]
        
        //发送请求
        request(LZTNetworkToolsMethod.POST, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
    //请求个人信息
    func oauthLoadUserInfo(userAccount: LZTUserAccountModel, success:(response: AnyObject?)->(), failure:(error: NSError)->()) {
        
        //url
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //请求参数,参数已经获取了，因此可以强解包
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        
        request(LZTNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
}

//MARK: - 请求天气接口
extension LZTNetworkTools {
    func getWeatherData(success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        request(LZTNetworkToolsMethod.GET, urlString: urlString, parameters: nil, success: success, failure: failure)
    }
}
